//
//  MWTreeSeedDetailsController.m
//  XiRan-iOS
//
//  Created by liuyiming on 2019/12/4.
//  Copyright © 2019 Wood. All rights reserved.
//

#import "MWTreeSeedDetailsController.h"
#import <WebKit/WebKit.h>

@interface MWTreeSeedDetailsController () <WKUIDelegate, WKNavigationDelegate>

@property(nonatomic, strong) UILabel *titleLB;
@property(nonatomic, strong) UILabel *timeLB;
@property(nonatomic, strong) WKWebView *webView;

@end

@implementation MWTreeSeedDetailsController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = [[MWConfigManager manager] objectForKey:@"treetype_info" defaultValue:@"树种介绍"];
    
    [self setupPageSubviews];
    [self loadTreeSeedWithID:_treeId];
}

#pragma mark - 请求树种详情
- (void)loadTreeSeedWithID:(NSInteger)treeId {
    NSMutableDictionary *paraDic = [[NSMutableDictionary alloc] init];
    [paraDic setObject:[MWUserManager manager].user.token forKey:@"token"];
    [paraDic setObject:[NSString stringWithFormat:@"%ld", treeId] forKey:@"id"];
    [paraDic setObject:[MWConfigManager manager].language forKey:@"language"];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hud.label.text = [[MWConfigManager manager] objectForKey:@"loadin" defaultValue:@"加载中..."];
    
    MWWeakSelf
    [[MWNetworkManager manager] POST:kTreeSeedDetailsUrl parameters:paraDic progress:^(NSProgress *downloadProgress) {
        
    } success:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSInteger code = [[dic objectForKey:@"code"] integerValue];
        NSString *msg = [dic objectForKey:@"msg"];
        
        if (code == 1) {
            NSDictionary *dataDic = [dic objectForKey:@"data"];
            MWTreeSeed *model = [MWTreeSeed mj_objectWithKeyValues:dataDic];
            weakSelf.titleLB.text = model.title;
            weakSelf.timeLB.text = model.createtime;
            [weakSelf.webView loadHTMLString:model.content baseURL:nil];
            
            [hud hideAnimated:YES];
            
        } else {
            hud.mode = MBProgressHUDModeText;
            hud.label.text = msg;
            [hud hideAnimated:YES afterDelay:2];
        }
        self.errorImageView.hidden = self.errorTitleLabel.hidden = YES;
        self.errorTitleLabel.text = [[MWConfigManager manager] objectForKey:@"click_retry" defaultValue:@"点击重试"];
        self.timeLB.hidden = self.titleLB.hidden = self.webView.hidden = NO;

    } failure:^(NSError *error) {
        [hud hideAnimated:YES];
        if ([MWConfigManager manager].isNetError) {
            self.errorImageView.hidden = self.errorTitleLabel.hidden = NO;
            self.errorTitleLabel.text = [[MWConfigManager manager] objectForKey:@"click_retry" defaultValue:@"点击重试"];
            self.timeLB.hidden = self.titleLB.hidden = self.webView.hidden = YES;
        }
    }];
}

- (void)refresh {
    [super refresh];
    [self loadTreeSeedWithID:_treeId];
}

- (void)setupPageSubviews {
    _titleLB = [[UILabel alloc] init];
    _titleLB.numberOfLines = 0;
    _titleLB.font = MWFont(17);
    _titleLB.textColor = MWHexColor(@"#333333");
    [self.view addSubview:_titleLB];
    
    _timeLB = [[UILabel alloc] init];
    _timeLB.font = MWFont(12);
    _timeLB.textColor = MWHexColor(@"#999999");
    [self.view addSubview:_timeLB];
    
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    WKPreferences *preference = [[WKPreferences alloc]init];
    preference.javaScriptEnabled = YES;
    preference.javaScriptCanOpenWindowsAutomatically = YES;
    config.preferences = preference;
    
    _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config];
    [_webView.scrollView setShowsVerticalScrollIndicator:NO];
    [_webView.scrollView setShowsHorizontalScrollIndicator:NO];
    _webView.UIDelegate = self;
    _webView.navigationDelegate = self;
    _webView.allowsBackForwardNavigationGestures = YES;
    [self.view addSubview:self.webView];
    
    [_titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(13);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
    }];
    
    [_timeLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLB.mas_bottom).offset(10);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
    }];
    
    [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.timeLB.mas_bottom).offset(10);
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
        make.bottom.equalTo(self.view);
    }];
}

@end
